package cn.qiao.demo.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

import java.lang.reflect.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * desc:
 *
 * @author qiao
 * @date 2023年12月13日 20:02
 */
public class ClassDemo {


    public static void main(String[] args) throws Exception {

        String s = getString();
        JSONObject jsonObject = JSONObject.parseObject(s);
        Method queryPushSuccessRecordByPage = Test.class.getMethod("queryPushSuccessRecordByPage", int.class, int.class);
        Type genericReturnType = queryPushSuccessRecordByPage.getGenericReturnType();
        Type[] actualTypeArguments = ((ParameterizedTypeImpl) genericReturnType).getActualTypeArguments();
        Type rawType = ((ParameterizedType) genericReturnType).getRawType();
        String typeName = rawType.getTypeName();
        Class<?> aClass = Class.forName(typeName);
        Object o = ((ParameterizedTypeImpl) genericReturnType).getRawType().newInstance();
        Method[] methods = o.getClass().getMethods();
        Field[] fields = o.getClass().getDeclaredFields();
        for (Method method : methods) {
            String name = method.getName();
            if (name.contains("set")){
                Class<?>[] parameterTypes = method.getParameterTypes();
                for (Class<?> parameterType : parameterTypes) {
                    TypeVariable<Method>[] typeParameters = method.getTypeParameters();
                    String name1 = parameterType.getName();
                }
                Type parameterType = method.getParameterTypes()[0];
                if (parameterType instanceof ParameterizedType){
                    System.out.println(1);
                }
                String set = name.replace("set", "");
                Method[] methods1 = aClass.getMethods();
                for (Method method1 : methods1) {
                    if (method1.getName().equals(name)) {
                        TypeVariable<Method>[] typeParameters = method1.getTypeParameters();
                        System.out.println(typeParameters);
                    }
                }

                String field = lowercaseFirstLetter(set);
                Object object = jsonObject.getObject(field, parameterTypes[0]);
                Object invoke = method.invoke(o, object);
                System.out.println(name);
            }

            Class<?>[] parameterTypes = method.getParameterTypes();

        }

        System.out.println(rawType);
    }

    // 获取泛型签名信息
    private static String getGenericSignature(Type type) {
        if (type instanceof TypeVariable) {
            // 如果是类型变量
            TypeVariable<?> typeVariable = (TypeVariable<?>) type;
            return typeVariable.getName();
        } else if (type instanceof Class) {
            // 如果是普通类
            Class<?> clazz = (Class<?>) type;
            return clazz.getSimpleName();
        } else {
            // 其他情况
            return type.getTypeName();
        }
    }


    // 判断类型是否为泛型类型
    private static boolean isGenericParameter(Class<?> parameterType) {
        Type[] genericInterfaces = parameterType.getGenericInterfaces();
        Type genericSuperclass = parameterType.getGenericSuperclass();

        // 判断是否是泛型类型
        return genericInterfaces.length > 0 || genericSuperclass != null;
    }

    private static String lowercaseFirstLetter(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }

        // 将字符串的第一个字符转为小写
        char firstChar = Character.toLowerCase(str.charAt(0));

        // 构建新的字符串
        return firstChar + str.substring(1);
    }

    private static String getString() {
        PunishPageList riskctrlResult = new PunishPageList<>();
        ArrayList<PubishSuccessDto> objects = new ArrayList<PubishSuccessDto>();
        for (int i = 0; i < 2; i++) {
            PubishSuccessDto pubishSuccessDto = new PubishSuccessDto();
            pubishSuccessDto.setRoleId(""+i);
            pubishSuccessDto.setGmtCreateFrom("时间"+i);
            pubishSuccessDto.setGmtCreateTo("GmtCreateTo"+i);
            pubishSuccessDto.setGmtExecutFrom("setGmtExecutFrom");
            String s = LocalDateTime.now().toString();
            pubishSuccessDto.setGmtExecutTo(s);
            objects.add(pubishSuccessDto);
        }
        riskctrlResult.setData(objects);
        Paginator paginator = new Paginator();
        paginator.setAge(1);
        paginator.setItems(20);
        paginator.setItemsPages(10);
        riskctrlResult.setPaginator(paginator);
        riskctrlResult.setErrorContext("errorContext");
        riskctrlResult.setSuccess(true);
        return JSON.toJSONString(riskctrlResult);
    }


}
